package com.wlyuan.open.constants;

import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lijing
 */
@Getter
public enum BillingUnit {

    KG("千克", 1),
    T("吨", 2),
    CBM("立方米", 3),
    LTR("升", 4),
    EACH("个", 5),
    PALLET("托", 6),
    SET("台", 7),
    CONTAINER("箱", 8),
    BALE("包", 9),
    BUNDLE("捆", 10),
    VEHICLE("辆", 11),
    PIECE("件", 12),
    BAG("袋", 13),
    FRAME("架", 14),
    BOX("盒", 15),
    BUCKET("桶", 16),
    OTHER("其它", 17),
    ;

    private final String name;
    private final Integer value;

    BillingUnit(String name, Integer value) {
        this.value = value;
        this.name = name;
    }

    private static final Map<Integer, BillingUnit> MAP = Arrays.asList(BillingUnit.values()).stream().collect(Collectors.toMap(BillingUnit::getValue, Function.identity()));

    public static BillingUnit fromValue(Integer value) {
        return Optional.ofNullable(value).map(a -> Optional.ofNullable(MAP.get(a)).orElse(null)).orElse(null);
    }
}
